/**
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var url_tmpl = "http://code.google.com/p/%PROJECT%/";
var title_tmpl = "%PROJECT% project information";

var prefs_obj = new gadgets.Prefs();
var projectName_str = "";

/**
 * Actually render the content of this gadget.
 *
 * @param response_str  The page to scrape.
 */
function render(response_str) {
  var response_obj = jQuery(response_str);

  // It's easier to grab each section at a time, out of order
  var title_str = "<tr><th valign='top' align='left'>Project Homepage:</th></tr><tr><td><a href='%HOMEPAGE_URL%' target='_blank'>%HOMEPAGE_LABEL%</a></td></tr>";
  var license_str = "<tr><th valign='top' align='left'>Code License:</th></tr><tr><td><a href='%HREF%' target='_blank'>%LICENSE%</a></td></tr>";
  var downloads_str = "<tr><th colspan='2' valign='top' align='left'>Featured downloads:</th></tr><tr><td><ul style='list-style:none; padding: 0; margin: 0;'>%DOWNLODS%</ul></td></tr>";
  var links_str = "<tr><th valign='top' align='left'>Links:</th></tr><tr><td><ul style='list-style:none; padding: 0; margin: 0;'>%LINKS%</ul></td></tr>";
  var blogs_str = "<tr><th valign='top' align='left'>Blogs:</th></tr><tr><td><ul style='list-style:none; padding: 0; margin: 0;'>%BLOGS%</ul></td></tr>";
  var groups_str = "<tr><th valign='top' align='left'>Groups:</th></tr><tr><td><ul style='list-style:none; padding: 0; margin: 0;'>%GROUPS%</ul></td></tr>";

  // The license, links, blogs, and groups sections are all .pmeta
  // Grab each one and modify links to work properly
  response_obj.find(".pmeta").each(function(i) {
    jQuery("tr", this).each(function(i) {
      var title_str = jQuery("th", this).text();
      switch(title_str) {
        case "Code License:":
          license_str = license_str.replace("%HREF%", jQuery("a", this).attr("href"))
                        .replace("%LICENSE%", jQuery("td", this).text());
          break;
        case "Links:":
          jQuery("ul a", this).each(function(i) {
            jQuery(this).attr("target", "_blank");
          });
          links_str = links_str.replace("%LINKS%", jQuery("ul", this).html());
          break;
        case "Blogs:":
          jQuery("ul a", this).each(function(i) {
            jQuery(this).attr("target", "_blank");
          });
          blogs_str = blogs_str.replace("%BLOGS%", jQuery("ul", this).html());
          break;
        case "Groups:":
          jQuery("ul a", this).each(function(i) {
            jQuery(this).attr("target", "_blank");
          });
          groups_str = groups_str.replace("%GROUPS%", jQuery("ul", this).html());
          break;
        default:
          break;
      }
    });
  });

  // If anything wasn't matched, empty the string
  license_str = license_str.search(/%LICENSE%/) >= 0 ? "" : license_str;
  links_str = links_str.search(/%LINKS%/) >= 0 ? "" : links_str;
  blogs_str = blogs_str.search(/%BLOGS%/) >= 0 ? "" : blogs_str;
  groups_str = groups_str.search(/%GROUPS%/) >= 0 ? "" : groups_str;

  // Grab each download and rebuild the a tag
  var eachDownload_str = "";
  response_obj.find("#downloadbox a:gt(0)").each(function(i) {
    eachDownload_str += "<li><a href='" + jQuery(this).attr("href") + "'>" + jQuery(this).text() + "</a></li>";
  });
  downloads_str = eachDownload_str == "" ? "" : downloads_str.replace("%DOWNLODS%", eachDownload_str);

  title_str = title_str.replace(/%HOMEPAGE_URL%/, "http://code.google.com/p/" + _esc(prefs_obj.getString("projectName")));
  title_str = title_str.replace(/%HOMEPAGE_LABEL%/, "http://code.google.com/p/" + _hesc(prefs_obj.getString("projectName")));

  // Assemble the whole table and insert into #content_div
  var content_str = "<center><table cellspacing='0' cellpadding='0'>" + title_str + license_str + downloads_str + links_str + blogs_str + groups_str + "</table></center>";
  $("#content_div").html(content_str);

  adjustIFrameHeight();
}

gadgets.util.registerOnLoadHandler(init);
